package com.google.code.UtopiaXml;

/**
 * Class representing xs:all indicator
 *
 * @author Michael Simacek
 */
public class SingleAll extends SingleIndicator {

    public SingleAll() {
    }

    /**
     * Creates SingleAll with given name
     *
     * @param name name
     */
    @Deprecated
    public SingleAll(String name) {
    }

    @Override
    public void generateParserBlock(CodeBuilder cb, String parentObject, String nodeName) {
        cb.appendLine("//begin SingleAll");
        cb.appendLine("do {");
        for (IndicatorContent item : getContent()) {
            StringBuilder cond = new StringBuilder();
            cond.append("if (").append(nodeName).append(" != null && (");
            for (String match : item.getMatchStrings()) {
                cond.append("\"").append(match).append("\"").append(".equals(")
                        .append(nodeName).append(".getNodeName()").append(") || ");
            }
            if (cond.length() > 3) {
                cond.setLength(cond.length() - 4);
            }
            cond.append(")) {");
            cb.appendLine(cond.toString());
            item.generateParserBlock(cb, parentObject, nodeName);
            cb.appendLine("}");
        }

        StringBuilder cond = new StringBuilder(nodeName + " != null && (");
        for (String match : getMatchStrings()) {
            cond.append("\"").append(match).append("\"").append(".equals(").append(nodeName).append(".getNodeName()) || ");
        }
        if (cond.length() > 3) {
            cond.setLength(cond.length() - 4);
        }
        cond.append(")");
        cb.appendLine("} while (" + cond + ");");

        cb.appendLine("//end SingleAll");
    }

    @Override
    protected Indicator shallowCopy() {
        return new SingleAll();
    }
}
